<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class Comment extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_order_comment';


    protected $append = [

        'lable_text',

        'order_goods',


    ];

    /**
     * @author chenniang
     * @DataTime: 2021-03-19 17:05
     * @功能说明:子订单信息
     */

    public function getOrderGoodsAttr($value,$data){

        if(!empty($data['order_id'])){

            $order_goods_model = new OrderGoods();

            $dis = [

                'order_id' => $data['order_id']
            ];

            $list = $order_goods_model->where($dis)->select()->toArray();

            return $list;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-07-05 23:32
     * @功能说明:标签列表
     */
    public function getLableTextAttr($vaule,$data){

        if(!empty($data['id'])){

            $lable_model = new Lable();

            $dis = [

                'b.comment_id' => $data['id'],

                'a.status'     => 1

            ];
            $list = $lable_model->alias('a')
                    ->join('massage_service_comment_lable b','a.id = b.lable_id')
                    ->where($dis)
                    ->column('a.title');

            return array_values($list);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

      $data = $this->alias('a')
             ->join('massage_service_order_list b','a.order_id = b.id')
             ->join('massage_service_order_goods_list c','a.order_id = c.order_id')
             ->join('massage_service_coach_list d','b.coach_id = d.id')
             ->join('massage_service_user_list e','a.user_id = e.id')
             ->where($dis)
             ->field('a.*,e.nickName,e.avatarUrl,c.goods_name,c.goods_cover,c.num,c.price,d.coach_name')
             ->group('a.id')
             ->order('a.id desc')
             ->paginate($page)
             ->toArray();

      return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        if(empty($data)){

            $this->dataAdd($dis);

            $data = $this->where($dis)->find();

        }

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-04-01 09:51
     * @功能说明:公众号楼长端新订单通知
     */
    public function sendMsg($order){

        $cap_model = new Cap();

        $cap_id = $order['cap_id'];

        $uniacid = $order['uniacid'];
        //获取楼长openid
        $openid = $cap_model->capOpenid($cap_id);

        $store_name = $cap_model->where(['id'=>$cap_id])->value('store_name');

        $access_token = longbingGetAccessToken($uniacid);

        $config = $this->dataInfo(['uniacid'=>$uniacid]);

        $page = "master/pages/order/list";
        //post地址
        $url  = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$access_token}";

        $x_config = longbingGetAppConfig($uniacid);

        $goods_data = '';

        $num = count($order['order_goods'])-1;

        foreach ($order['order_goods'] as $k=>$v){

            $icon = $k==$num?'':',';

            $goods_data .= $v['goods_name'].'x'.$v['goods_num'].$icon;
        }

        $order_text = !empty($order['text'])?','.$order['text']:'';

        $address_data = $order['address_info'];

        $data = [
            //用户小程序openid
            'touser' => $openid,

            'mp_template_msg' => [
                //公众号appid
                'appid' => $config['app_id'],

                "url"   => "http://weixin.qq.com/download",
                //公众号模版id
                'template_id' => $config['tmp_id'],

                'miniprogram' => [
                    //小程序appid
                    'appid'   => $x_config['appid'],
                    //跳转小程序地址
                    'page'    => $page,
                ],
                'data' => array(

                    'first' => array(

                        'value' => $store_name.'商家，您有一笔新订单',

                        'color' => '#93c47d',
                    ),

                    'keyword1' => array(

                        'value' => $order['order_code'],

                        'color' => '#93c47d',
                    ),
                    'keyword2' => array(
                        //内容
                        'value' => $goods_data.$order_text,

                        'color' => '#0000ff',
                    ),
                    'keyword3' => array(
                        //内容
                        'value' => $order['pay_price'].'元',

                        'color' => '#0000ff',
                    ),
                    'keyword4' => array(
                        //内容
                        'value' => '送货上门',

                        'color' => '#0000ff',
                    ),
                    'keyword5' => array(

                        'value' => $address_data['user_name'].','.$address_data['mobile'].','.$address_data['address'].$address_data['address_info'],

                        'color' => '#45818e',
                    ),
                    'remark' => array(
                        //内容
                        'value' => $order['order_text'],

                        'color' => '#0000ff',
                    ),
                )
            ],
        ];


        $data = json_encode($data);

        $tmp = [

            'url'  => $url,

            'data' => $data,
        ];
        $rest = lbCurlPost($tmp['url'], $tmp['data']);

        $rest = json_decode($rest, true);

        return $rest;

    }









}